package com.cardbaobao.cardbabyclient.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.callback.RecycleItemClickedCallback;
import com.cardbaobao.cardbabyclient.ui.fragment.CreditFragment;
import com.cardbaobao.cardbabyclient.model.CreditCardIndexLayout;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;


/**
 * Created by Jiangzx on 11:42.
 */

public class HotRecDelegateAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder>{
    private List<CreditCardIndexLayout.HotRec> data;
    private LayoutHelper mSingleLayoutHelper;
    private int radian;
    public HotRecDelegateAdapter(LayoutHelper layoutHelper,List<CreditCardIndexLayout.HotRec> data){
        this.mSingleLayoutHelper=layoutHelper;
        this.data=data;
        this.radian=DensityUtil.dp2px(6.0f);
    }

    private RecycleItemClickedCallback clickedCallback;
    public void setRecycleItemClickedCallback(RecycleItemClickedCallback clickedCallback){
        this.clickedCallback=clickedCallback;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mSingleLayoutHelper;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_credit_card_hot_rec,parent,false);
        return new HotRecHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(data.size()>=3&&holder instanceof HotRecHolder) {
            HotRecHolder hotRecHolder = (HotRecHolder) holder;
            hotRecHolder.tvHotRec.setText("热门推荐");
            CreditCardIndexLayout.HotRec left=data.get(0);

            hotRecHolder.tvHotRecLeftTitle.setText(left.getName());
            hotRecHolder.tvHotRecLeftContent.setText(left.getMarketing_one());
            ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+left.getPicture_path(),
                    hotRecHolder.ivHotRecLeft,
                    R.mipmap.img_default_rectangle,
                    radian);
            hotRecHolder.rlHotRecLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(clickedCallback!=null){
                        clickedCallback.onRecycleItemClicked(0);
                    }
                }
            });

            CreditCardIndexLayout.HotRec rightTop=data.get(1);
            hotRecHolder.tvHotRecRightTopTile.setText(rightTop.getName());
            hotRecHolder.tvHotRecRightTopContent.setText(rightTop.getMarketing_one());
            ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+rightTop.getPicture_path(),
                    hotRecHolder.ivHotRecRightTop,
                    R.mipmap.img_default_rectangle,
                    radian);
            hotRecHolder.rlHotRecRightTop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(clickedCallback!=null){
                        clickedCallback.onRecycleItemClicked(1);
                    }
                }
            });

            CreditCardIndexLayout.HotRec rightBottom=data.get(2);
            hotRecHolder.tvHotRecRightBottomTitle.setText(rightBottom.getName());
            hotRecHolder.tvHotRecRightBottomContent.setText(rightBottom.getMarketing_one());
            ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+rightBottom.getPicture_path(),
                    hotRecHolder.ivHotRecRightBottom,
                    R.mipmap.img_default_rectangle,
                    radian);
            hotRecHolder.rlHotRecRightBottom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(clickedCallback!=null){
                        clickedCallback.onRecycleItemClicked(2);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    @Override
    public int getItemViewType(int position) {
        return CreditFragment.TYPE_HOT_REC;
    }

    //热门推荐viewHolder
    private class HotRecHolder extends RecyclerView.ViewHolder {
        private LinearLayout llCreditHotRecContainer;//热门推荐
        private RelativeLayout rlHotRecLeft;
        private TextView tvHotRec;//热门推荐标签
        private TextView tvHotRecLeftTitle;//热门推荐左边标题
        private TextView tvHotRecLeftContent;//热门推荐左边内容
        private ImageView ivHotRecLeft;//热门推荐左边图片

        private RelativeLayout rlHotRecRightTop;
        private TextView tvHotRecRightTopTile;//热门推荐右上标题
        private TextView tvHotRecRightTopContent;//热门推荐右上内容
        private ImageView ivHotRecRightTop;//热门推荐右上图片

        private RelativeLayout rlHotRecRightBottom;
        private TextView tvHotRecRightBottomTitle;//热门推荐右下标题
        private TextView tvHotRecRightBottomContent;//热门推荐右下内容
        private ImageView ivHotRecRightBottom;//热门推荐右下图片

        private HotRecHolder(View itemView) {
            super(itemView);
            llCreditHotRecContainer = (LinearLayout) itemView.findViewById(R.id.ll_hot_rec_container);
            //热门推荐左
            rlHotRecLeft= (RelativeLayout) llCreditHotRecContainer.findViewById(R.id.rl_hot_rec_left);
            tvHotRec =
                    (TextView) llCreditHotRecContainer.findViewById(R.id.tv_hot_rec);
            tvHotRecLeftTitle =
                    (TextView) llCreditHotRecContainer.findViewById(R.id.tv_hot_rec_left_title);
            tvHotRecLeftContent =
                    (TextView) llCreditHotRecContainer.findViewById(R.id.tv_hot_rec_left_content);
            ivHotRecLeft =
                    (ImageView) llCreditHotRecContainer.findViewById(R.id.iv_hot_rec_left);
            //热门推荐右上
            rlHotRecRightTop= (RelativeLayout) llCreditHotRecContainer.findViewById(R.id.rl_hot_rec_right_top);
            tvHotRecRightTopTile =
                    (TextView) llCreditHotRecContainer.findViewById(R.id.tv_hot_rec_right_top_title);
            tvHotRecRightTopContent =
                    (TextView) llCreditHotRecContainer.findViewById(R.id.tv_hot_rec_right_top_content);
            ivHotRecRightTop =
                    (ImageView) llCreditHotRecContainer.findViewById(R.id.iv_hot_rec_right_top);
            //热门推荐右下
            rlHotRecRightBottom= (RelativeLayout) llCreditHotRecContainer.findViewById(R.id.rl_hot_rec_right_bottom);
            tvHotRecRightBottomTitle =
                    (TextView) llCreditHotRecContainer.findViewById(R.id.tv_hot_rec_right_bottom_title);
            tvHotRecRightBottomContent =
                    (TextView) llCreditHotRecContainer.findViewById(R.id.tv_hot_rec_right_bottom_content);
            ivHotRecRightBottom =
                    (ImageView) llCreditHotRecContainer.findViewById(R.id.iv_hot_rec_right_bottom);
        }
    }
}
